<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Admin\Sys;

use App\Controller\AbstractController;
use App\Middleware\AuthMiddleware;
use App\Model\Message;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;

#[Middlewares([AuthMiddleware::class])]
#[Controller]
class MessageController extends AbstractController
{
    #[GetMapping(path: '/sys/message/list')]
    public function list()
    {
        $pageNum = $this->request->input('pageNum', 1);
        $pageSize = $this->request->input('pageSize', 10);
        $where = [];


        $list = Message::query()->when($where, function ($query) use ($where) {
            return $query->where($where);
        })->forPage($pageNum, $pageSize)->get();
        $data['searchCount'] = true;
        $data['current'] = (int)$pageNum;
        $data['size'] = (int)$pageSize;
        $data['total'] = Message::query()->count();
        $data['pages'] = ceil($data['total'] / $pageSize);
        $data['records'] = $list;
        return success($data);
    }


    #[DeleteMapping(path: '/sys/message/delete/{ids}')]
    public function delete($ids)
    {
        $idArr = explode(',', $ids);
        Message::query()->whereIn('id',  $idArr)->delete();
        return success();
    }
}
